package com.mr.security.web.commons.validate;

import com.mr.security.web.commons.exception.ValidateCodeException;
import com.mr.security.web.commons.utils.ValidateCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Class: ValidateCodeProcessorHolder
 * User (作者):MRui
 * TODO (描述)：验证码处理器处理人（用来处理调用哪中验证码的实现类）
 * Date 2019-01-26 12:13
 */
@Slf4j
@Component
public class ValidateCodeProcessorHolder {

    @Autowired
    private Map<String, IValidateCodeProcessor> validateCodeProcessors;

    /**
    * User (作者): MRui
    * TODO (描述)：根据传入的类型返回验证码处理器（供validateCodeFilter调用）
    * Data：2019-01-26 12:20
    */
    public IValidateCodeProcessor findValidateCodeProcessor(ValidateCodeEnum type) {
        log.info("type={}",type);
        return findValidateCodeProcessor(type.toString().toLowerCase());
    }
    /**
    * User (作者): MRui
    * TODO (描述)：根据传入的类型返回验证码处理器
    * Data：2019-01-26 12:27
    */
    public IValidateCodeProcessor findValidateCodeProcessor(String type) {
        String name = type.toLowerCase() + IValidateCodeProcessor.class.getSimpleName();
        log.info("IValidateCodeProcessor:name={}",name);
        IValidateCodeProcessor processor = validateCodeProcessors.get(name);
        if (processor == null) {
            log.info("验证码处理器name={}不存在",name);
            throw new ValidateCodeException("验证码处理器" + name + "不存在");
        }
        return processor;
    }

}
